<template>
  <div id="space-nav">
    <div class="btns">
      <div :id="swiperId" ref="swiper" class="swiper-container space-btn">
        <!-- :style="isShowControlBtn ? `width:${swiperWidth}px` : ''" -->
        <div class="swiper-wrapper">
          <div
            v-for="(space, index) in titles"
            :key="index"
            class="btn-item swiper-slide swiper-no-swiping"
            :class="{ active: index == showIndex }"
            @click="handleClick(space, index)"
            :title="space.name"
            @mouseover="handleMouseOver(space, index)"
            @mouseleave="handleMouseLeave()"
          >
            {{ space.name }}
            <div class="lv2" :style="{ display: currentIndex == index ? 'block' : 'none' }">
              <div
                class="lv2_item"
                v-for="item in space.child"
                @click="clickEvt(item)"
                :key="item.name"
                >{{ item.name }}</div
              >
            </div>
          </div>
        </div>
      </div>
      <div class="control-btn" v-if="isShowControlBtn">
        <div :class="{ disabled: spaceActiveIndex === 0 }" @click="handleLeft">&lt;</div>
        <div :class="{ disabled: isLastPage }" @click="handleRight">&gt;</div>
      </div>
    </div>
  </div>
</template>

<script>
  import emitter from '@/utils/emitter';
  //每个空间的宽度
  const ITEM_WIDTH = 140;
  //右侧切换按钮宽度
  const BTN_WIDTH = 60;
  //距右侧按钮的距离
  const SKEW = 40;
  import swiper from '@/mixins/swiper';
  export default {
    name: 'SpaceNav',
    mixins: [swiper],
    props: {
      titles: {
        type: Array
      }
    },
    data() {
      return {
        currentIndex: 0,
        swiperId: `swiper${this._uid}`,
        showIndex: 0,
        //是否显示右侧切换按钮
        isShowControlBtn: false,
        swiperWidth: 0,
        isLastPage: false,
        spaceActiveIndex: 0
      };
    },
    mounted() {
      this.isSwiperInit();
      emitter.subscribe('resize', () => {
        this.reInitSwiper();
      });
    },
    methods: {
      // 点击二级菜单进行跳转方法
      clickEvt(item) {
        if (item.url) {
          window.open(item.url);
        }
      },
      // 鼠标移出导航栏
      handleMouseLeave() {
        this.currentIndex = 0;
      },
      // 鼠标移到导航栏
      handleMouseOver(space, index) {
        this.currentIndex = index;
        console.log(index);
      },
      // 判断空间是否需要翻页
      isSwiperInit() {
        const columnWidth = Math.floor(document.getElementById(this.swiperId).offsetWidth);
        const length = this.titles.length;
        if (length * ITEM_WIDTH > columnWidth) {
          //如果数量大于栏目id就显示切换按钮。初始化swiper
          this.isShowControlBtn = true;
          this.swiperWidth = Math.floor(columnWidth - BTN_WIDTH - SKEW);
          this.$nextTick(() => {
            this.initSwiper(this.swiperWidth / ITEM_WIDTH);
          });
        }
      },
      // 重新渲染Swiper
      reInitSwiper() {
        this._swiper && this._swiper.destroy(true);
        this.isLastPage = false;
        this.spaceActiveIndex = 0;
        this.isSwiperInit();
      },
      /**
       * @description:初始化siper
       * @param {}",
       * @return ,
       */
      initSwiper(slidesPerGroup) {
        this.setSwiper(
          new Swiper(`#${this.swiperId}`, {
            loop: false,
            speed: 300,
            slidesPerView: slidesPerGroup,
            slidesPerGroup,
            noSwiping: true,
            onSlideChangeStart: (swiper) => {
              //不是第一个。并且最后一个元素是显示的。就认为他是翻到最后一页了
              this.isLastPage =
                swiper.activeIndex !== 0 &&
                swiper.getLastSlide().className.includes('swiper-slide-visible');
            },
            onSlideChangeEnd: (swiper) => {
              this.spaceActiveIndex = swiper.activeIndex;
            }
          })
        );
      },
      /**
       * @description 向左调幻灯片
       */
      handleLeft() {
        this._swiper.swipePrev();
      },
      /**
       * @description 向右调幻灯片
       */
      handleRight() {
        if (this.isLastPage) return;
        this._swiper.swipeNext();
      },
      handleClick(space, index) {
        if (this.showIndex == index) {
          return;
        }
        // fixme: id为当前tab绑定的id
        emitter.publish('changeSpace', space);
        this.showIndex = index;
      }
    }
  };
</script>

<style lang="scss" scoped>
  * {
    padding: 0;
    margin: 0;
    list-style: none;
  }

  .swiper-no-swiping {
    position: relative;
  }

  .swiper-wrapper {
    display: flex;
    height: 48px;
    padding-right: 25px;
    padding-left: 10px;
    font-size: 20px;
    font-weight: 600;
    box-sizing: border-box;
    justify-content: space-between;
  }

  .lv2 {
    position: absolute;
    top: 48px;
    left: 0;
    z-index: 999;
    width: 100%;
    background: #fff;
  }

  .lv2_item {
    width: 100%;
    height: 40px;
    font-size: 18px;
    line-height: 40px;
    color: #000;
    text-align: center;
    cursor: pointer;
    background-color: #fff;
  }

  .lv2_item:hover {
    color: #fff;
    background-color: #2490f8;
  }

  .swiper-container {
    overflow: inherit;
  }

  #space-nav {
    position: relative;
    width: 100%;
    height: 100%;

    .swiper-container {
      z-index: 9;
      height: 100%;
      // margin: 0;
      // margin-right: 65px;
    }
  }

  .swiper-slide {
    width: 120px !important;
    height: 100%;
    margin-left: 0 !important;
  }

  .swiper-slide:nth-child(1) {
    margin-left: 10px;
  }

  .btns {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: inherit;
    // overflow: hidden;

    .space-btn {
      // width: 94%;

      .btn-item {
        width: 100px;
        // height: inherit;
        // display: inline-block;
        padding: 0 23px;
        // overflow: hidden; //溢出隐藏
        line-height: 50px;
        text-align: center;
        text-overflow: ellipsis;
        white-space: nowrap; //禁止换行
        cursor: pointer;

        &:hover {
          background-color: rgba(0, 0, 0, 0.5);
        }
      }
    }

    .control-btn {
      position: absolute;
      right: 0;
      bottom: 0;
      z-index: 9;
      height: 100%;
      line-height: 50px;
      color: #fff;
      text-align: center;
      user-select: none;

      > div {
        display: inline-block;
        width: 30px;
        font-size: 20px;
        cursor: pointer;

        &.disabled {
          cursor: not-allowed;
          opacity: 0.3;
        }
      }
    }
  }

  .show-content {
    width: 100%;
    height: 30px;
    padding-top: 50px;
  }

  .active {
    background-color: rgba(0, 0, 0, 0.5);
  }
</style>
